package com.kayak.weixin.util;

import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONException;
import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.rpc.RpcException;
import com.kayak.dubbo.bean.ConsumerBean;
import com.kayak.dubbo.bean.RpcBean;
import com.kayak.exception.ErrorException;
import com.kayak.kkutils.JsonUtil;
import com.kayak.kkweb.sys.SysBeans;
import com.kayak.service.msg.weixin.mp.GetMpAccessTokenInterface;
import com.kayak.service.msg.weixin.mp.GetWinxinMpCfgInterface;
import com.kayak.service.msg.weixin.mp.SetMpAccessTokenInterface;
import com.kayak.weixin.msg.template.TemplateMessage;
import com.kayak.weixin.pojo.Token;

public class WeixinAdvancedUtil {

	public final static Logger log = LoggerFactory.getLogger(WeixinAdvancedUtil.class);
	
/*	*//**
	 * 根据mpid获取微信公众号配置信息
	 * @param mpid
	 * @return
	 * @throws ErrorException
	 *//*
	public  Map<String,Object> getMpcfgByMpid(String mpid) throws ErrorException{
		ConsumerBean consumerBean = new ConsumerBean();
		consumerBean.addRequest("mpid", mpid);
		RpcBean rpcBean=null;
		GetWinxinMpCfgInterface  getWinxinMpCfgService = (GetWinxinMpCfgInterface) SysBeans.getBean("getWinxinMpCfgService");//获取微信公众号配置信息
		try {
			rpcBean=getWinxinMpCfgService.execute(consumerBean);
			CommonUtil.verifyServiceReturn(rpcBean);
		} catch (RpcException e) {
			e.printStackTrace();
			throw new ErrorException(ErrorCodeWeixin.ERROR_EASYSP_F990_CODE,ErrorCodeWeixin.ERROR_EASYSP_F990_MSG);
		}
		Map<String,Object> mpMap = rpcBean.getResponse();
		return mpMap;
	}
	
	*//**
	 * 根据appid获取access_token
	 * @param appid
	 * @param appsecret
	 * @return
	 * @throws ErrorException
	 *//*
	public  Map<String,Object> getAccessTokenByAppid(String appid,String appsecret) throws ErrorException{
		ConsumerBean consumerBean = new ConsumerBean();
		consumerBean.addRequest("appid", appid);
		consumerBean.addRequest("appsecret", appsecret);
		RpcBean rpcBean=null;
		try {
			GetMpAccessTokenInterface  getMpAccessTokenService = (GetMpAccessTokenInterface) SysBeans.getBean("getMpAccessTokenService");//获取access_token
			rpcBean=getMpAccessTokenService.execute(consumerBean);
			CommonUtil.verifyServiceReturn(rpcBean);
		} catch (RpcException e) {
			e.printStackTrace();
			throw new ErrorException(ErrorCodeWeixin.ERROR_EASYSP_F990_CODE,ErrorCodeWeixin.ERROR_EASYSP_F990_MSG);
		}
		Map<String,Object> tokenMap = new HashMap<String,Object>();
		if(rpcBean.getResponse("access_token_json")==null||"".equals(rpcBean.getResponse("access_token_json"))){
			//重新获取access_token
			Token token = null;
			
			String access_token_url = WeixinConst.ACCESS_TOKEN_URL;
			String requestUrl = access_token_url.replace("APPID", appid).replace("APPSECRET", appsecret);
			// 发起GET请求获取凭证
			JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "GET", null);
			if (null != jsonObject) {
				try {
					token = new Token();
					token.setAccessToken(jsonObject.getString("access_token"));
					token.setExpiresIn(jsonObject.getInt("expires_in"));
				} catch (JSONException e) {
					token = null;
					// 获取token失败
					log.error("获取token失败 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg"));
				}
			}
			String tokenjson = JsonUtil.objectToJson(token);
			//保存access_token
			ConsumerBean consumerBean2 = new ConsumerBean();
			consumerBean2.addRequest("appid", appid);
			consumerBean2.addRequest("access_token_json", tokenjson);
			RpcBean rpcBean2=null;
			SetMpAccessTokenInterface  setMpAccessTokenService = (SetMpAccessTokenInterface) SysBeans.getBean("setMpAccessTokenService");//存储access_token
			try {
				rpcBean2=setMpAccessTokenService.execute(consumerBean2);
				CommonUtil.verifyServiceReturn(rpcBean2);
			} catch (RpcException e) {
				e.printStackTrace();
				throw new ErrorException(ErrorCodeWeixin.ERROR_EASYSP_F990_CODE,ErrorCodeWeixin.ERROR_EASYSP_F990_MSG);
			}
			tokenMap = (Map<String, Object>) JsonUtil.jsonToMap(tokenjson);
		}else{
			tokenMap = (Map<String, Object>) JsonUtil.jsonToMap(rpcBean.getResponse("access_token_json"));
		}
		return tokenMap;
	}
	
	*//**
	 * 根据mpid获取access_token
	 * @param mpid
	 * @return
	 * @throws ErrorException
	 *//*
	public  Map<String,Object> getAccessTokenByMpid(String mpid) throws ErrorException{
		
		Map<String,Object> mpMap = getMpcfgByMpid(mpid);
		String appid = mpMap.get("appid").toString();
		String appsecret = mpMap.get("appsecret").toString();
		
		return getAccessTokenByAppid(appid,appsecret);
	} 
	
	*//**
	 * 发送模板消息
	 * 
	 * @param accessToken 接口访问凭证
	 * @param jsonMsg json格式的模板消息
	 * @return ret_code 返回码
	 * @throws ErrorException 
	 *//*
	public  Map<String,String> sendTemplateMessage(String mpid, TemplateMessage templatemsg) throws ErrorException {
		Map<String,String> returnMap= new HashMap<String,String>();
		String ret_code = "";
		String msgid ="";
		// 拼接请求地址
		String requestUrl = WeixinConst.SEND_TEMPLATE_MSG_URL;
		String access_token = getAccessTokenByMpid(mpid).get("accessToken").toString();
		requestUrl = requestUrl.replace("ACCESS_TOKEN", access_token);
		// 发送模板消息
		JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "POST", JsonUtil.objectToJson(templatemsg));

		if (null != jsonObject) {
			int errorCode = jsonObject.getInt("errcode");
			String errorMsg = jsonObject.getString("errmsg");
			if (0 == errorCode) {
				msgid = jsonObject.getString("msgid");
				returnMap.put("msgid", msgid);
				returnMap.put("ret_code", "0000");
				log.info("【模板消息发送成功】 errcode:{} errmsg:{} msgid:{}", errorCode, errorMsg, msgid);
			} else if (43004 == errorCode) { //需要接收者关注
				ret_code = ""+errorCode;
				returnMap.put("msgid", msgid);
				returnMap.put("ret_code", ret_code);
				log.error("【未关注公众号发送失败】 errcode:{} errmsg:{}", errorCode, errorMsg);
			} else{
				ret_code = ""+errorCode;
				returnMap.put("msgid", msgid);
				returnMap.put("ret_code", ret_code);
				log.error("【模板消息发送失败】 errcode:{} errmsg:{}", errorCode, errorMsg);
			}
		}
		return returnMap;
	}
*/
}
